package com.wgj.android.adapter;

import android.content.Context;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiaai.xui.utils.XDateUtils;
import com.jiaai.xui.utils.imageload.XImage;
import com.wgj.android.R;
import com.wgj.android.entity.Group;
import com.wgj.android.entity.WeiXingGroup;
import com.wgj.android.http.weixin.WeiXinCacheUtil;
import com.wgj.android.utils.MD5Util;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/12/10.
 */

public class WeiXinGroupAdapter extends  BaseAdapter<WeiXingGroup,WeiXinGroupAdapter.GroupHolder> {
    private  static  Context mContext;
    private static String basePath;
    private List<WeiXingGroup> groups=new ArrayList<>();
    public WeiXinGroupAdapter(Context context,List<WeiXingGroup> groups) {
        super(context);
        if (WeiXinCacheUtil.getInfo() != null){
            basePath = Environment.getExternalStorageDirectory()+"/wgj"+ File.separator+"weixing"+ File.separator+ WeiXinCacheUtil.getInfo().getWxuin()+File.separator;
        }
        this.mContext=context;
        this.groups=groups;
    }

    @Override
    public void addItem(WeiXingGroup group) {
        groups.add(group);
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged(List<WeiXingGroup> dataList) {
        groups.clear();
        groups.addAll(dataList);
        notifyDataSetChanged();
    }


    @Override
    public WeiXinGroupAdapter.GroupHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new WeiXinGroupAdapter.GroupHolder(LayoutInflater.from(mContext).inflate(R.layout.weixing_group_item,parent,false));
    }

    @Override
    public void onBindViewHolder(WeiXinGroupAdapter.GroupHolder holder, int position) {
        holder.bindData(groups.get(position));
    }

    @Override
    public int getItemCount() {
        return groups.size();
    }

    static class GroupHolder extends RecyclerView.ViewHolder  {
        TextView weixingroup;
        ImageView head;
        TextView number;

        public GroupHolder(View itemView) {
            super(itemView);
            weixingroup=itemView.findViewById(R.id.weixingroup);
            head=itemView.findViewById(R.id.head);
            number=itemView.findViewById(R.id.number);

        }

        public void bindData(WeiXingGroup group){
            weixingroup.setText(group.getNickName());
            String path=basePath.concat(MD5Util.encrypt(group.getNickName()));
            number.setText(group.getNumber()+"人");
            Glide.with(mContext).load(new File(path)).placeholder(R.mipmap.img_default_head).into((ImageView) head);
        }
    }
}
